<?php

namespace JsonLowCode\UI;

use JsonLowCode\Schema;
use JsonLowCode\Traits\Props\HasAffix;

/**
 * 组件 - 侧边导航 AnchorLink
 * @method static AnchorLink make(string $title = null, string $href = "", array|callable $options = []) AnchorLink组件
 */
class AnchorLink extends Schema
{
    protected string $componentName = "NAnchorLink";

    use HasAffix;

    private array $options = [];

    /**
     * @param string|null $title
     * @param string $href
     * @param array|callable $options
     */
    public function __construct(string $title = null, string $href = "", array|callable $options = [])
    {
        parent::__construct();
        $this->title($title);
        $this->href($href);
        $this->options($options);
    }

    /**
     * 设置标题
     * @param string $title
     * @return $this
     */
    public function title(string $title): AnchorLink
    {
        return $this->prop('title', $title);
    }

    /**
     * 设置跳转ID锚点
     * @param string $href
     * @return $this
     */
    public function href(string $href): AnchorLink
    {
        return $this->prop('href', $href);
    }

    /**
     * 设置子数据
     * @param array $options
     * @return $this
     */
    public function options(array $options = []): AnchorLink
    {
        $children = [];
        if (is_callable($options)){
            call_user_func($options,$this);
        }
        if (is_array($options)){
            foreach ($options as $option){
                $children[] = new self($option['title'], $option['href'],$option['children']??[]);
            }
        }
        return $this->children($children);
    }

}
